<template>
  <!-- 添加或修改菜单对话框 -->
  <el-dialog :title="!form.id ? '新增' : '修改'"
             :visible.sync="visible">
    <el-form ref="dataForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="上级参数">
            <treeselect v-model="form.parentId"
                        :options="paramOptions"
                        :normalizer="normalizer"
                        :show-count="true"
                        placeholder="上级参数"
            />
          </el-form-item>
          <el-form-item label="参数名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入参数名称"/>
          </el-form-item>
          <el-form-item label="此参数值" prop="value">
            <el-input v-model="form.value" placeholder="请输入此参数值"/>
          </el-form-item>
          <el-form-item label="缩略图" prop="img" >
            <avue-input-icon v-model="form.img" :icon-list="iconList"></avue-input-icon>
          </el-form-item>
          <el-form-item label="参数状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio-button label="ZT10001">显示</el-radio-button>
              <el-radio-button label="ZT10002">隐藏</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="排序" prop="sequence">
            <el-input-number v-model="form.sequence" controls-position="right" :min="0"/>
          </el-form-item>
          <el-form-item label="备注说明" prop="content">
            <el-input type="textarea" v-model="form.content" placeholder="备注说明"/>
          </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {fetchList, fetchParamTree, getObj, addObj, putObj, delObj} from '@/api/sdAdmin/param'
  import {listByParent} from '@/api/sdAdmin/syslocation'
  import Treeselect from "@riophae/vue-treeselect"
  import iconList from '@/const/iconList'
  import TableForm from './'
  import "@riophae/vue-treeselect/dist/vue-treeselect.css"

  export default {
    name: "param-form",
    components: {Treeselect, TableForm},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 菜单树选项
        paramOptions: [],
        // 是否显示弹出层
        visible: false,
        // 图标
        iconList: iconList,
        form: {
          id: undefined,
          name: undefined,
          value: undefined,
          parentId: undefined,
          status: 'ZT10001',
          sequence: 999,
          content: undefined
        },
        // 表单校验
        rules: {
          name: [
            {required: true, message: "参数标题不能为空", trigger: "blur"}
          ],
          sequence: [
            {required: true, message: "排序不能为空", trigger: "blur"}
          ],
          value: [
            {required: true, message: "参数值不能为空", trigger: "blur"}
          ]
        }
      };
    },
    mounted () {
      this.getTreeselect()
    },
    methods: {
      init(isEdit, id) {
        if (id != null) {
          this.form.parentId = id;
        }
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (isEdit) {
            getObj(id).then(response => {
              this.form = response.data.data
            })
          } else {
            this.form.id = undefined
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        console.log(this.form)
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.form.parentId === undefined) {
              this.form.parentId = -1
            }

            if (this.form.id) {
              putObj(this.form).then(data => {
                this.$message.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              });
            } else {
              addObj(this.form).then(data => {
                this.$message.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              })
            }
          }
        })
      },
      /** 查询参数下拉树结构 */
      getTreeselect() {
        fetchParamTree(false).then(response => {
          this.paramOptions = [];
          const param = {id: -1, name: '根参数', children: []};
          param.children = response.data.data;
          this.paramOptions.push(param);
        });
      },
      /** 转换参数数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.id,
          label: node.name,
          children: node.children
        };
      }
    }
  };
</script>
